package uno.component;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CardValue {
	
	//tipi di carta
	public static final int PLUS_FOUR = 0;
	public static final int PLUS_TWO = 1;
	public static final int CHANGE_FLOW = 2;
	public static final int ONE_STOP = 3;
	public static final int CHANGE_COLOR = 4;
	public static final int NO_EFFECT = 5;
	
	//colori possibili
	public static final String YELLOW = "yellow";
	public static final String RED = "red";
	public static final String BLUE = "blue";
	public static final String GREEN = "green";
	public static final String NO_COLOR = "no color";
	
	//
	
	private String color;
	private int number;
	private int type;
	private BufferedImage cardImage;
	
	/**
	 * 
	 * @param color colore della carta
	 * @param type tipo di carta
	 * */
	public CardValue(String color, int type, int number) {
		super();
		this.color = color;
		this.number = number;
		this.type = type;
		this.cardImage =  readImage();
	}
	
	/**
	 * 
	 * Costruttore per le carte +2, che non possiedono un numero
	 * 
	 * @param color colore della carta
	 * @param type tipo di carta
	 * */
	public CardValue(int type) {
		super();
		this.color = NO_COLOR;
		this.number = 0;
		this.type = type;
		this.cardImage =  readImage();
	}
	
	/**
	 * 
	 * Costruttore per le carte senza colore e numero
	 * 
	 * @param color colore della carta
	 * @param type tipo di carta
	 * */
	public CardValue(String color, int type) {
		super();
		this.color = color;
		this.number = 0;
		this.type = type;
		this.cardImage =  readImage();
	}
	

	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}



	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}



	public int getType() {
		return type;
	}

	/**
	 * @return the number
	 */
	public int getNumber() {
		return number;
	}



	/**
	 * @param number the number to set
	 */
	public void setNumber(int number) {
		this.number = number;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "type: " + type +", color: " + color +", number: " + number;
	}

	
	private String buildImagePathFromAttributes() {

		String imgPath = null;
		
		switch(type) {

		case CardValue.CHANGE_FLOW:

			imgPath = "img/cards/inv-" + color +".png";
			break;

		case CardValue.ONE_STOP:

			imgPath = "img/cards/pas-" + color +".png";
			break;

		case CardValue.CHANGE_COLOR:

			imgPath = "img/cards/jok.png";
			break;

		case CardValue.PLUS_TWO:
			
			imgPath = "img/cards/+2-" + color +".png";
			break;

		case CardValue.PLUS_FOUR:
			
			imgPath = "img/cards/+4.png";
			break;
			
		case CardValue.NO_EFFECT:
			
			imgPath = "img/cards/" + number + "-" + color +".png";

		}
		


		return imgPath;

	}

	/**
	 * Legge l'immagine al corrispondente path.
	 * Se non la trova torna null
	 * */
	private BufferedImage readImage(){

		//Pezzo di codice per caricare le immagini
		String imgPath = buildImagePathFromAttributes();
		//System.out.println("leggendo l'immagine " + imgPath);
		BufferedImage bufImg = null;
		try {
			bufImg = ImageIO.read(new File(imgPath));
		} catch (IOException e) {
			
			//e.printStackTrace();
			System.out.println("Nessuna carta corrispondente all'immagine " + imgPath);
		}
		
		return bufImg;

	}

	public BufferedImage getCardImage() {
		return cardImage;
	}
}
